package ch03.jdkProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Jane
 * @date 2024-03-25 16:48
 */

public class JdkProxyFactory2 {
    public static <T>T getInstance(Object target){// target 目标
    /*    ClassLoader loader,
                Class<?>[] interfaces,
                InvocationHandler h*/
      return (T) Proxy.newProxyInstance(target.getClass().getClassLoader()
              , target.getClass().getInterfaces()
              , new InvocationHandler() {

                  @Override
                  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                      before();
                      Object obj = method.invoke(proxy, args);
                      System.out.println(obj);
                      after();
                      return obj;
                  }
              });

    }

    public static void before(){
        System.out.println("买之前先货比三家");
    }
    public static void after(){
        System.out.println("早买早享受");
    }
}
